package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.domain.SoftwareLink;
import com.ruoyi.web.domain.SoftwareListVO;
import com.ruoyi.web.domain.SoftwareVersion;
import com.ruoyi.web.domain.Softwares;
import com.ruoyi.web.service.ISoftwareLinkService;
import com.ruoyi.web.service.ISoftwareQueryService;
import com.ruoyi.web.service.ISoftwareVersionService;
import com.ruoyi.web.service.ISoftwaresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Anonymous
@RequestMapping("/software/query")
public class SoftwareQueryController extends BaseController {

    @Autowired
    private ISoftwaresService softwaresService;
    @Autowired
    private ISoftwareVersionService softwareVersionService;
    @Autowired
    private ISoftwareLinkService softwareLinkService;
    @Autowired
    private ISoftwareQueryService softwareQueryService;

    /**
     * 查询软件列表
     */
//    @PreAuthorize("@ss.hasPermi('software:query:softwareList')")
    @GetMapping("/softwareList")
    public TableDataInfo list(Softwares softwares) {
        clearPage();
        softwares.setAliasName(softwares.getSoftwareName());
        List<Softwares> list = softwaresService.selectSoftwaresList(softwares);
        return getDataTable(list);
    }

    /**
     * 查询软件版本列表
     * 根据软件ID对版本列表查询
     */
//    @PreAuthorize("@ss.hasPermi('software:query:versionList')")
    @GetMapping("/versionList")
    public TableDataInfo list(SoftwareVersion softwareVersion) {
        clearPage();
        List<SoftwareVersion> list = softwareVersionService.selectSoftwareVersionList(softwareVersion);
        return getDataTable(list);
    }

    /**
     * 查询下载链接列表
     * 根据软件ID和版本ID查询链接列表
     */
//    @PreAuthorize("@ss.hasPermi('software:query:linkList')")
    @GetMapping("/linkList")
    public TableDataInfo list(SoftwareLink softwareLink) {
        clearPage();
        List<SoftwareLink> list = softwareLinkService.selectSoftwareLinkList(softwareLink);
        return getDataTable(list);
    }

    /**
     * 所有软件列表查询
     */
//    @PreAuthorize("@ss.hasPermi('software:query:list')")
    @GetMapping("/list")
    public TableDataInfo list(SoftwareListVO listVO) {
        clearPage();
        List<SoftwareListVO> list = softwareQueryService.softwareQueryList(listVO);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 修改软件
     */
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody SoftwareVersion softwareVersion)
    {
        List<SoftwareListVO> list = softwareQueryService.softwareQueryList(null);
        ArrayList<SoftwareVersion> softwareVersions = new ArrayList<>();
        SoftwareVersion s = null;

        for (SoftwareListVO softwareListVO : list){
            s = new SoftwareVersion();
            s.setVersionId(softwareListVO.getVersionId());
            s.setAliasName(softwareListVO.getSoftwareName()+softwareListVO.getVersionName());
            softwareVersions.add(s);
            softwareVersionService.updateSoftwareVersion(s);
        }
        return null;
//        return toAjax(softwareVersionService.updateSoftwareVersion(softwareVersion));
    }

}
